package com.tf_cut.admin.agent.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.tf_cut.admin.agent.vo.AgentAccountDetailVo;
import com.tf_cut.admin.agent.vo.AgentAccountVo;
import com.tf_cut.admin.agent.vo.AgentAndAccountVo;
import com.tf_cut.admin.annotation.Decrypt;
import com.tf_cut.admin.annotation.Encrypt;
import com.tf_cut.admin.backend.api.BackEndApi;
import com.tf_cut.admin.common.pojo.PageInfo;
import com.tf_cut.admin.common.pojo.Result;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/agent/account/")
@Slf4j
public class AgentAccountController {
	@Autowired
	private BackEndApi agentApi;
	
	
	// 进入代理商账号清单页面
	@GetMapping("accountlist")
    public ModelAndView agentList() {
        return new ModelAndView("agent/account/accountlist", "agent", null);
    }
	
	// 进入代理商账号加款页面
	@GetMapping("addmoney/{id}")
    public ModelAndView addMoney(@PathVariable("id")String agentAccountId) {
		AgentAccountVo avo = AgentAccountVo.builder()
				.agentAccountId(agentAccountId)
				.direction(1)
				.build();
        return new ModelAndView("agent/account/addmoney", "agent", avo);
    }
	
	// 进入代理商账号交易流水页面
	@GetMapping("detaillist/{id}")
    public ModelAndView detailList(@PathVariable("id")String agentAccountId) {
		AgentAccountDetailVo avo = AgentAccountDetailVo.builder()
				.agentAccountId(agentAccountId)
				.build();
        return new ModelAndView("agent/account/detaillist", "agent", avo);
    }
	
	// 进入代理商账号扣款页面
	@GetMapping("delmoney/{id}")
    public ModelAndView delMoney(@PathVariable("id")String agentAccountId) {
		AgentAccountVo avo = AgentAccountVo.builder()
				.agentAccountId(agentAccountId)
				.direction(-1)
				.build();
        return new ModelAndView("agent/account/delmoney", "agent", avo);
    }
	
	// 代理商清单页面翻页
	@SuppressWarnings("unchecked")
	@PostMapping("accountlist/page")
	@Decrypt
    @Encrypt
    public Result<PageInfo<AgentAndAccountVo>> page(AgentAndAccountVo avo) {
	
        String json = JSON.toJSONString(avo);
        json = agentApi.findListAgent(json);
        return Result.of(JSON.parseObject(json, PageInfo.class));
    }
	
	// 代理商账号交易明细页面翻页
	@SuppressWarnings("unchecked")
	@PostMapping("detaillist/page")
	@Decrypt
    @Encrypt
    public Result<PageInfo<AgentAccountDetailVo>> pageDetail(AgentAccountDetailVo avo) {
	
        String json = JSON.toJSONString(avo);
        json = agentApi.findAgentAccountDetail(json);
        return Result.of(JSON.parseObject(json, PageInfo.class));
    }
	
	// 代理商调额 加款（1）/ 扣款（-1） 
	@SuppressWarnings("unchecked")
	@PostMapping("operation")
	@Decrypt
    @Encrypt
	public Result<Boolean> operation(AgentAccountVo avo) {
		return JSON.parseObject(agentApi.operationAgentAccount(avo.getAgentAccountId(),
				avo.getOperationMoney(),avo.getDirection(),avo.getTradeComment()),Result.class);
	}
}
